package com.example.client;

import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import entity.User;

public class ContenWrite extends AppCompatActivity implements View.OnClickListener {
    private Button btn_save;
    private Button btn_read;
    private Button btn_del;
    private EditText et_name;
    private EditText et_age;
    private EditText et_height;
    private EditText et_weight;
    private CheckBox ck_married;
    private String path;
    private TextView tv_show;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_conten_write);
        btn_save =  findViewById(R.id.btn_save);
        btn_read  =findViewById(R.id.btn_read);
        btn_del = findViewById(R.id.btn_del);
        et_age = findViewById(R.id.et_age);
        et_height = findViewById(R.id.et_height);
        et_weight = findViewById(R.id.et_weight);
        et_name = findViewById(R.id.et_name);
        ck_married = findViewById(R.id.check_is);
        tv_show = findViewById(R.id.tv_show);

        btn_read.setOnClickListener(this);
        btn_save.setOnClickListener(this);
        btn_del.setOnClickListener(this);
    }

    @SuppressLint("Range")
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_save){
            ContentValues values = new ContentValues();
            values.put(UserInfoContent.USER_NAME,et_name.getText().toString());
            values.put(UserInfoContent.USER_AGE,Integer.parseInt(et_age.getText().toString()));
            values.put(UserInfoContent.USER_HEIGHT,Integer.parseInt(et_height.getText().toString()));
            values.put(UserInfoContent.USER_WEIGHT,Float.parseFloat(et_weight.getText().toString()));
            values.put(UserInfoContent.USER_MARRIED,ck_married.isChecked());
            getContentResolver().insert(UserInfoContent.CONTENT_URI,values);
            Toast.makeText(this,"保存成功",Toast.LENGTH_LONG);
        }else if(v.getId() == R.id.btn_read){
            Cursor cursor = getContentResolver().query(UserInfoContent.CONTENT_URI, null, null, null);
            if (cursor!=null){
                while (cursor.moveToNext()){
                    User info = new User();
                    //info.id = cursor.getInt(cursor.getColumnIndex(UserInfoContent._ID));
                    info.name = cursor.getString(cursor.getColumnIndex(UserInfoContent.USER_NAME));
                    info.height = cursor.getInt(cursor.getColumnIndex(UserInfoContent.USER_HEIGHT));
                    info.weight = cursor.getFloat(cursor.getColumnIndex(UserInfoContent.USER_WEIGHT));
                    info.married = cursor.getInt(cursor.getColumnIndex(UserInfoContent.USER_MARRIED)) == 1?true:false;
                    info.age = cursor.getInt(cursor.getColumnIndex(UserInfoContent.USER_AGE)) ;
                    Log.e("TAG", "onClick: " + info);
                }
            }
        }else if (v.getId() == R.id.btn_del){
            Uri uri = ContentUris.withAppendedId(UserInfoContent.CONTENT_URI,2);
            int count = getContentResolver().delete(uri,null,null);
            //int count = getContentResolver().delete(UserInfoContent.CONTENT_URI, "name=?", new String[]{"zs"});
            if (count > 0){
                Toast.makeText(this,"删除成功",Toast.LENGTH_LONG);
            }
        }
    }
}